import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { SellPromotionManageService } from '../sell-promotion-manage.service';
import { PermissionService } from 'src/app/service/permission.service';
import { GoodsLabelManageService } from '../../../goods/goods-label-list/goods-label-manage.service';

@Component({
  selector: 'app-direct-descent-manage',
  templateUrl: './direct-descent-manage.component.html',
  styleUrls: ['./direct-descent-manage.component.scss']
})
export class DirectDescentManageComponent implements OnInit {

  seachParams: any = {
    name: '',
    status: '', // 状态：1、未开始 2、进行中 3、已结束 4、已停止 5 草稿 6、待审核 7、审核不通过
    type: '' // 活动类型：1、满减型  2、折扣型
  };
  // 状态：1、未开始 2、进行中 3、已结束 4、已停止 5 草稿 6、待审核 7、审核不通过
  statusOptions: any = [
    { value: 1, label: '未开始' },
    { value: 2, label: '进行中' },
    { value: 3, label: '已结束' },
    { value: 4, label: '已停止' },
    { value: 5, label: '草稿' }
  ]

  // table
  tableForms: any = {
    tableLoading: false,
    page: 1,
    pageSize: 20,
    total: 0
  };
  // 表格data
  tableData: any = [1, 2];

  constructor(
    private services: SellPromotionManageService,
    private message: NzMessageService,
    private goodsLabelManageService: GoodsLabelManageService,
    public permission: PermissionService
  ) { }

  ngOnInit(): void {
    this.loadTable();
  }

  async loadTable() {
    try {
      let params = {
        ...this.seachParams,
        page: this.tableForms.page,
        pageSize: this.tableForms.pageSize
      }

      this.tableForms.tableLoading = true;
      const res: any = await this.services.queryPromotionListData(params);
      this.tableForms.tableLoading = false;
      if (res.code == 0) {
        const { records, total }: any = res.data;
        this.tableData = records || [];
        this.tableForms.total = total || 0;
      } else {
        this.message.error(res.message || '数据获取失败');
      }
    } catch (error: any) {
      this.tableForms.tableLoading = false;
      this.message.error(error);
      this.tableData = [];
      this.tableForms.total = 0;
    }
  }

  // 查询
  queryData() {
    this.tableForms.page = 1;
    this.loadTable();
  }
  // 重置
  resetData() {
    for (let key in this.seachParams) {
      this.seachParams[key] = '';
    }
    this.tableForms.page = 1;
    this.loadTable();
  }

  /**
   * 停止 | 删除
   * @param num 0：停止，1：删除
   * @param item
   * 状态：1、未开始 2、进行中 3、已结束 4、已停止 5 草稿 6、待审核 7、审核不通过 8删除 
   */
  async stopDelete(num: number, item: any) {
    let params: any = {
      id: item.id
    }
    if (num == 0) {
      params.status = 4;
    } else {
      params.status = 8;
    }
    try {
      const res: any = await this.services.updateSalesPromotionData(params);
      if (res.code == 0) {
        this.message.success((num == 0 ? '停止活动' : '删除活动') + '成功');
        this.loadTable();
      } else {
        this.message.error(res.message || (num == 0 ? '停止活动' : '删除活动') + '失败');
      }
    } catch (error: any) {
      this.message.error(error);
    }
  }

  // 当前页码改变时的回调函数
  onPageIndexChange(index: number) {
    this.tableForms.page = index;
    this.loadTable();
  }
  // 页数改变时回调
  onPageSizeChange(sizeNum: number) {
    this.tableForms.page = 1;
    this.tableForms.pageSize = sizeNum;

    this.loadTable();
  }

  // 生成商品标签
  async generateGoodLable(item: any) {
    let params = {
      promotionId: item.id
    }
    
    const res: any = await this.goodsLabelManageService.addEditGoodsLabelData(params, 0);
    if (res.code === 0) {
      this.message.success('成功生成商品标签');
      this.loadTable();
    } else {
      this.message.error(res.message);
    }
  }
}
